package ThreadDemo;

public class DaemonDemo {
    public static void main(String[] args) {
        A a = new A();
        B b = new B();
        Thread t = new Thread() {
            @Override
            public void run() {
                a.rous();
            }
        };

        Thread t1 = new Thread() {
            @Override
            public void run() {
                b.jack();
            }
        };

        t.start();
        /*
         * 守护线程的设置必须在start方法开始之前
         * */
        t1.setDaemon(true);
        t1.start();
    }
}

class A {
    public void rous() {
        for (int i = 0; i < 5; i++) {
            try {
                Thread.sleep(1000);
                System.out.println("用户线程正在运行第" + (i + 1));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("rous进程结束");
    }
}

class B {
    public void jack() {
        while (true) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("守护进程jack正在运行！");
        }
    }
}
